<?php

namespace App\Http\Controllers\Steel;

use App\Http\Controllers\BaseController;
use App\Models\Steel\SteelMaterialPriceCustom;
use App\Models\Steel\SteelMaterialPriceCustomHistory;
use App\Models\Steel\SteelMaterialPriceSupply;
use App\Models\Steel\SteelMaterialPriceSupplyHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

/**
 * 基价相关
 */
class SteelMaterialPriceController extends BaseController
{
    /**
     * 获取基价历史列表
     */
    public function getlisthistory(Request $request)
    {
        //需获取当前用户所属账套
        $type = $request->type ?? 1; //1业务端，2采购端

        if ($type == 1) {
            $query = SteelMaterialPriceCustomHistory::where(['cpid' => $this->mangerinfo['cpid']])->orderBy('update_at', 'desc');
        } else {
            $query = SteelMaterialPriceSupplyHistory::where(['cpid' => $this->mangerinfo['cpid']])->orderBy('update_at', 'desc');
        }

        $limit = $request->limit ?? 10;
        $material_attr_name = $request->material_attr_name;
        if ($material_attr_name != '') {
            $query->where('material_attr_name', 'like', '%' . $material_attr_name . '%');
        }


        $material_attr_no = $request->material_attr_no;
        if ($material_attr_no != '') {
            $query->where('material_attr_no', 'like', '%' . $material_attr_no . '%');
        }
        $list = $query->select(['id', 'material_attr_name', 'material_attr_no', 'base_price', 'add_price', 'price', 'create_at', 'update_at'])->paginate($limit)->toArray();
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => paginateDataToArray($list)]);
    }

    /**
     * 基价列表
     */
    public function getlist(Request $request)
    {
        //需获取当前用户所属账套
        $type = $request->type ?? 1; //1业务端，2采购端

        if ($type == 1) {
            $query = SteelMaterialPriceCustom::where(['cpid' => $this->mangerinfo['cpid']])->orderBy('update_at', 'desc');
        } else {
            $query = SteelMaterialPriceSupply::where(['cpid' => $this->mangerinfo['cpid']])->orderBy('update_at', 'desc');
        }

        $limit = $request->limit ?? 10;
        $material_attr_name = $request->material_attr_name;
        if ($material_attr_name != '') {
            $query->where('material_attr_name', 'like', '%' . $material_attr_name . '%');
        }


        $material_attr_no = $request->material_attr_no;
        if ($material_attr_no != '') {
            $query->where('material_attr_no', 'like', '%' . $material_attr_no . '%');
        }
        $list = $query->select(['id', 'material_attr_name', 'material_attr_no', 'base_price', 'add_price', 'price', 'create_at', 'update_at'])->paginate($limit)->toArray();
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => paginateDataToArray($list)]);
    }

    /**
     * 修改基价
     */
    public function dochangeprice(Request $request)
    {
        $type = $request->type ?? 1; //1业务端，2采购端
        $price_id = $request->price_id ?? 0; //基价记录主键

        $base_price = intval(bcmul(($request->base_price ?? 0), 100, 0)); //基价
        $add_price = intval(bcmul(($request->add_price ?? 0), 100, 0)); //加价

        $base_price = intval($request->base_price); //基价
        $add_price = intval($request->add_price); //加价
        $price = bcadd($base_price, $add_price, 0);
        if ($base_price <= 0) {
            return response()->json(['code' => '001', 'msg' => '基价不能小于0']);
        }
        if ($type == 1) {
            $model = new SteelMaterialPriceCustom;
            $model_history = new SteelMaterialPriceCustomHistory;
            $cache_key = 'custom_material_price';
        } else {
            $model = new SteelMaterialPriceSupply;
            $model_history = new SteelMaterialPriceSupplyHistory;
            //产品经理的采购端不维护加价和单价
            $add_price = 0;
            $price = 0;
            $cache_key = 'supply_material_price';
        }

        $info = $model->where(['cpid' => $this->mangerinfo['cpid'], 'id' => $price_id])->first();

        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $model->where(['cpid' => $this->mangerinfo['cpid'], 'id' => $price_id])->update(['base_price' => $base_price, 'add_price' => $add_price, 'price' => $price]);
        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'smpc_id' => $info->id,
            'material_attr_id' => $info->material_attr_id,
            'material_attr_no' => $info->material_attr_no,
            'material_attr_name' => $info->material_attr_name,
            'base_price' => $base_price,
            'add_price' => $add_price,
            'price' => $price,
            'create_at' => time(),
            'update_at' => time()
        ];
        $model_history::create($data);

        //缓存记录价格
        Redis::set($cache_key . '_' . $info->material_attr_id, json_encode(['base_price' => $base_price, 'add_price' => $add_price, 'price' => $price]));

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
